// 武器类
class Weapon {
    constructor(type = 'rifle') {
        this.type = type;
        this.damage = 10;
        this.ammo = 30;
        this.maxAmmo = 30;
        this.reserveAmmo = 120;
        this.fireRate = 0.1;
        this.bulletSpeed = 100;
        this.spread = 0.01;
        this.lastFired = 0;
        
        // 根据武器类型设置属性
        this.setupWeaponType();
    }
    
    setupWeaponType() {
        switch(this.type) {
            case 'rifle':
                this.damage = 15;
                this.ammo = 30;
                this.maxAmmo = 30;
                this.reserveAmmo = 120;
                this.fireRate = 0.1;
                this.bulletSpeed = 100;
                this.spread = 0.01;
                break;
            case 'shotgun':
                this.damage = 8;
                this.ammo = 8;
                this.maxAmmo = 8;
                this.reserveAmmo = 32;
                this.fireRate = 0.8;
                this.bulletSpeed = 80;
                this.spread = 0.05;
                this.pellets = 8; // 霰弹枪特有：每次发射的弹丸数
                break;
            case 'sniper':
                this.damage = 50;
                this.ammo = 5;
                this.maxAmmo = 5;
                this.reserveAmmo = 20;
                this.fireRate = 1.5;
                this.bulletSpeed = 150;
                this.spread = 0.001;
                break;
            case 'machinegun':
                this.damage = 12;
                this.ammo = 100;
                this.maxAmmo = 100;
                this.reserveAmmo = 300;
                this.fireRate = 0.05;
                this.bulletSpeed = 90;
                this.spread = 0.02;
                break;
        }
    }
    
    canFire() {
        const currentTime = Date.now() / 1000;
        return this.ammo > 0 && currentTime - this.lastFired >= this.fireRate;
    }
    
    fire() {
        if (!this.canFire()) return false;
        
        this.lastFired = Date.now() / 1000;
        this.ammo--;
        
        return true;
    }
    
    reload() {
        const ammoNeeded = this.maxAmmo - this.ammo;
        const ammoToLoad = Math.min(ammoNeeded, this.reserveAmmo);
        
        if (ammoToLoad <= 0) return false;
        
        this.ammo += ammoToLoad;
        this.reserveAmmo -= ammoToLoad;
        
        return true;
    }
    
    getFireDirection(baseDirection) {
        // 根据武器散布计算实际发射方向
        const direction = baseDirection.clone();
        
        // 添加散布
        if (this.spread > 0) {
            const spreadX = (Math.random() - 0.5) * 2 * this.spread;
            const spreadY = (Math.random() - 0.5) * 2 * this.spread;
            
            // 创建横向和纵向的随机向量
            const up = new THREE.Vector3(0, 1, 0);
            const right = new THREE.Vector3().crossVectors(direction, up).normalize();
            const upNormalized = new THREE.Vector3().crossVectors(right, direction).normalize();
            
            // 应用散布
            direction.add(right.multiplyScalar(spreadX));
            direction.add(upNormalized.multiplyScalar(spreadY));
            direction.normalize();
        }
        
        return direction;
    }
    
    // 获取武器的视觉效果参数
    getVisualParams() {
        switch(this.type) {
            case 'rifle':
                return {
                    muzzleFlashSize: 0.1,
                    muzzleFlashColor: 0xffff00,
                    tracerLength: 10,
                    tracerWidth: 0.01,
                    tracerColor: 0xffff00
                };
            case 'shotgun':
                return {
                    muzzleFlashSize: 0.3,
                    muzzleFlashColor: 0xff6600,
                    tracerLength: 5,
                    tracerWidth: 0.005,
                    tracerColor: 0xff6600,
                    pellets: 8
                };
            case 'sniper':
                return {
                    muzzleFlashSize: 0.2,
                    muzzleFlashColor: 0xffffff,
                    tracerLength: 20,
                    tracerWidth: 0.005,
                    tracerColor: 0xffffff
                };
            case 'machinegun':
                return {
                    muzzleFlashSize: 0.15,
                    muzzleFlashColor: 0xffff33,
                    tracerLength: 8,
                    tracerWidth: 0.008,
                    tracerColor: 0xffff33
                };
            default:
                return {
                    muzzleFlashSize: 0.1,
                    muzzleFlashColor: 0xffff00,
                    tracerLength: 10,
                    tracerWidth: 0.01,
                    tracerColor: 0xffff00
                };
        }
    }
    
    // 获取武器的音效参数（如果有音频系统）
    getAudioParams() {
        switch(this.type) {
            case 'rifle':
                return {
                    fireSound: 'rifle_fire',
                    reloadSound: 'rifle_reload',
                    fireVolume: 0.8
                };
            case 'shotgun':
                return {
                    fireSound: 'shotgun_fire',
                    reloadSound: 'shotgun_reload',
                    fireVolume: 1.0
                };
            case 'sniper':
                return {
                    fireSound: 'sniper_fire',
                    reloadSound: 'sniper_reload',
                    fireVolume: 1.2
                };
            case 'machinegun':
                return {
                    fireSound: 'machinegun_fire',
                    reloadSound: 'machinegun_reload',
                    fireVolume: 0.9
                };
            default:
                return {
                    fireSound: 'default_fire',
                    reloadSound: 'default_reload',
                    fireVolume: 0.8
                };
        }
    }
    
    // 获取武器的伤害效果（击中敌人后的视觉效果）
    getHitEffectParams() {
        switch(this.type) {
            case 'rifle':
                return {
                    particleCount: 5,
                    particleSize: 0.05,
                    particleColor: 0xff0000,
                    effectDuration: 0.3
                };
            case 'shotgun':
                return {
                    particleCount: 10,
                    particleSize: 0.03,
                    particleColor: 0xff6600,
                    effectDuration: 0.4
                };
            case 'sniper':
                return {
                    particleCount: 15,
                    particleSize: 0.08,
                    particleColor: 0xff3333,
                    effectDuration: 0.5
                };
            case 'machinegun':
                return {
                    particleCount: 3,
                    particleSize: 0.04,
                    particleColor: 0xff0000,
                    effectDuration: 0.2
                };
            default:
                return {
                    particleCount: 5,
                    particleSize: 0.05,
                    particleColor: 0xff0000,
                    effectDuration: 0.3
                };
        }
    }
}

// 武器升级系统
class WeaponUpgrade {
    constructor(weapon) {
        this.weapon = weapon;
        this.upgradeLevel = 0;
        this.maxUpgradeLevel = 3;
    }
    
    upgrade() {
        if (this.upgradeLevel >= this.maxUpgradeLevel) return false;
        
        this.upgradeLevel++;
        
        // 根据武器类型应用升级
        switch(this.weapon.type) {
            case 'rifle':
                this.weapon.damage *= 1.2;
                this.weapon.fireRate *= 0.9;
                this.weapon.spread *= 0.9;
                break;
            case 'shotgun':
                this.weapon.damage *= 1.3;
                this.weapon.pellets += 2;
                this.weapon.spread *= 0.95;
                break;
            case 'sniper':
                this.weapon.damage *= 1.4;
                this.weapon.fireRate *= 0.95;
                this.weapon.spread *= 0.8;
                break;
            case 'machinegun':
                this.weapon.damage *= 1.15;
                this.weapon.ammo += 25;
                this.weapon.maxAmmo += 25;
                this.weapon.spread *= 0.95;
                break;
        }
        
        return true;
    }
    
    getUpgradeStats() {
        return {
            level: this.upgradeLevel,
            maxLevel: this.maxUpgradeLevel,
            damageMultiplier: Math.pow(1.2, this.upgradeLevel),
            isMaxLevel: this.upgradeLevel >= this.maxUpgradeLevel
        };
    }
}

// 武器管理器 - 处理武器切换和选择
class WeaponManager {
    constructor() {
        this.weapons = [];
        this.currentWeaponIndex = 0;
    }
    
    addWeapon(weapon) {
        this.weapons.push(weapon);
    }
    
    switchToNextWeapon() {
        if (this.weapons.length <= 1) return;
        
        this.currentWeaponIndex = (this.currentWeaponIndex + 1) % this.weapons.length;
        return this.getCurrentWeapon();
    }
    
    switchToPreviousWeapon() {
        if (this.weapons.length <= 1) return;
        
        this.currentWeaponIndex = (this.currentWeaponIndex - 1 + this.weapons.length) % this.weapons.length;
        return this.getCurrentWeapon();
    }
    
    switchToWeapon(index) {
        if (index >= 0 && index < this.weapons.length) {
            this.currentWeaponIndex = index;
            return this.getCurrentWeapon();
        }
        return null;
    }
    
    getCurrentWeapon() {
        return this.weapons[this.currentWeaponIndex] || null;
    }
    
    getWeaponCount() {
        return this.weapons.length;
    }
    
    getWeaponByType(type) {
        return this.weapons.find(weapon => weapon.type === type) || null;
    }
}